export const getNetworkType = theKey => new Promise((resolve, reject) => {
  wx.getNetworkType({
    success(res) {
      resolve(res)
    },
    fail(e) {
      reject(e)
    }
  })
})

export const checkSession = theKey => new Promise((resolve, reject) => {
  wx.checkSession({
    success(res) {
      resolve(res)
    },
    fail(e) {
      reject(e)
    }
  })
})

export const getStorage = theKey => new Promise((resolve, reject) => {
  wx.getStorage({
    key: theKey,
    success(res) {
      resolve(res.data)
    },
    fail(e) {
      reject(e)
    }
  })
})

export const setStorage = (theKey, theValue) => new Promise((resolve, reject) => {
  wx.setStorage({
    key: theKey,
    data: theValue,
    success() {
      resolve()
    },
    fail(e) {
      reject(e)
    }
  })
})

export const getLocation = () => new Promise((resolve) => {
  wx.getLocation({
    success({
      latitude,
      longitude,
      speed,
      accuracy
    }) {
      resolve({
        latitude,
        longitude,
        speed,
        accuracy
      })
    },
    fail() {
      resolve({
        latitude: -999,
        longitude: -999
      })
    }
  })
})

export const chooseLocation = () => new Promise((resolve, reject) => {
  wx.chooseLocation({
    success(res) {
      resolve(res)
    },
    fail(e) {
      reject(e)
    }
  })
})

export const removeStorage = theKey => new Promise((resolve, reject) => {
  wx.removeStorage({
    key: theKey,
    success(res) {
      resolve(res.data)
    },
    fail(e) {
      reject(e)
    }
  })
})

export const clearStorage = () => new Promise((resolve, reject) => {
  try {
    wx.clearStorageSync()
    resolve()
  } catch (e) {
    reject(e)
  }
})

export const login = () => new Promise((resolve, reject) => {
  wx.login({
    success(res) {
      resolve([null, res])
    },
    fail(e) {
      wx.showToast({
        title: '登陆失败，请重试',
        icon: false
      })
      resolve([e, null])
    }
  })
})

export const chooseImage = (count = 1, sourceType = ['album']) => new Promise((resolve, reject) => {
  wx.chooseImage({
    count,
    sourceType,
    success(res) {
      resolve(res)
    },
    fail(e) {
      reject(e)
    }
  })
})

export const showShareMenu = () => {
  return wx.showShareMenu
}

export const pageScrollTo = () => {
  return wx.pageScrollTo
}

export const getUserInfo = (obj) => new Promise((resolve, reject) => {
  wx.getUserInfo({
    ...obj,
    success(res) {
      resolve(res)
    },
    fail(e) {
      reject(e)
    }
  })
})

export const showLoading = (title, mask) => {
  if (title === false) {
    wx.hideLoading()
  } else {
    wx.showLoading({
      title,
      mask: mask || true,
      success() {
        wx.isLoading = true
      }
    })
  }
}

export const alert = (title, content, success) => {
  wx.showModal({
    title: title || ' ',
    content: content || ' ',
    showCancel: false,
    cancelColor: "#999999",
    confirmColor: "#37a3fe",
    success,
    fail() {
      console.log('取消')
    }
  })
}

export const switchTab = (url, gapTime) => {
  var app = getApp();
  if (app.globalData.isClicked) {
    return;
  }
  app.globalData.isClicked = true;
  wx.switchTab({
    url,
    complete: function (res) {
      let timer = setTimeout(() => {
        app.globalData.isClicked = false;
        clearTimeout(timer)
      }, 1000);
    },
  });
}

export const navigateTo = (url, gapTime) => {
  var app = getApp();
  if (app.globalData.isClicked) {
    return;
  }
  app.globalData.isClicked = true;
  wx.navigateTo({
    url,
    complete: function (res) {
      console.log(res);
      let timer = setTimeout(() => {
        app.globalData.isClicked = false;
        clearTimeout(timer)
      }, 1500);
    },
  });
}

export const ShowToast = (content, icon, duration) => {
  wx.showToast({
    title: content,
    icon: icon || "none",
    duration: duration || 1500
  });
}

export const redirectTo = (redirectUrl) => {
  wx.redirectTo({
    url: redirectUrl
  })
}

export const startPullDownRefresh = () => new Promise((resolve, reject) => {
  wx.startPullDownRefresh({
    success(res) {
      resolve(res)
    },
    fail(e) {
      reject(e)
    }
  })
})

export const stopPullDownRefresh = () => new Promise((resolve, reject) => {
  wx.stopPullDownRefresh({
    success(res) {
      resolve(res)
    },
    fail(e) {
      reject(e)
    }
  })
})

export const uploadFile = (obj) => new Promise((resolve, reject) => {
  wx.uploadFile({
    ...obj,
    success(res) {
      resolve(res)
    },
    fail(e) {
      reject(e)
    }
  })
})

export const canvasToTempFilePath = (target) => new Promise((resolve, reject) => {
  wx.canvasToTempFilePath({
    canvasId: target,
    success(res) {
      console.log('============', res.tempFilePath);
      resolve(res.tempFilePath)
    },
    fail(e) {
      console.log('------------', e);
      reject(e)
    }
  })
})

export default {
  checkSession,
  getNetworkType,
  getStorage,
  setStorage,
  getLocation,
  chooseLocation,
  removeStorage,
  clearStorage,
  login,
  chooseImage,
  showShareMenu,
  pageScrollTo,
  getUserInfo,
  showLoading,
  alert,
  switchTab,
  navigateTo,
  redirectTo,
  ShowToast,
  startPullDownRefresh,
  stopPullDownRefresh,
  uploadFile,
  canvasToTempFilePath
}
